import Vue from 'vue';
import VueRouter from 'vue-router';
/* routes */
import routes from '@src/router/routes'
import { RESOURCE_PREFIX } from '@src/util/linkSwitch'
import { checkRequestUrl } from '@src/api/UserCenterApi'
// 权限判断白名单
const whiteList = ['/auth/error','/calendar/error','/logout','/smlogin/pc/logout','/500','demo','/service/manage/provider/create']

Vue.use(VueRouter);

const router = new VueRouter({
  mode: 'history',
  base: RESOURCE_PREFIX,
  routes,
});

router.beforeEach((to, from, next) => {
  if(to.path === '/reload' && !location.pathname.includes('/reload')){
		// 语言切换使用空页面跳转
    const path = (location.pathname.replace(RESOURCE_PREFIX, '') || '/') + location.search
		next()
    setTimeout(() => {
      router.replace({path})
    }, 0);
		return
	}
  if(whiteList.indexOf(to.path)>-1 || to.path== '/' || to.path.indexOf('/v_open')>-1){
    next()
  }else{
    try {
      checkRequestUrl({requestUrl:to.path}).then(res=>{
        if(res.status === 0){
          return next()
        }
        next('/auth/error')
      }).catch(err=>{
        next('/auth/error')
      })
    } catch (error) {
      next('/auth/error')
    }
  }
})
export default router
